package com.crcode.crm.controller.home;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.crcode.crm.bean.user.UserBean;
import com.crcode.crm.service.meetingRecord.MeetingRecordService;
import com.crcode.crm.service.retrieval.RetrievalService;
import com.crcode.crm.service.role.RoleUserService;
import com.crcode.crm.service.system.NoticeService;
import com.crcode.crm.utils.PageVo;

/**
 * 首页
 * 
 * @author lxw
 * @date 2019年4月24日
 */
@Controller
public class HomeController {
	@Resource(name = "meetingRecordService")
	private MeetingRecordService meetingRecordService; //会议

	@Resource(name = "retrievalService")
	private RetrievalService retrievalService; //检索进度

	@Resource(name = "noticeService")
	private NoticeService noticeService; //公告
	
	@Autowired
	private RoleUserService roleUserService;
	
	@RequestMapping(value="/home/loadHomePageData")
	@ResponseBody
	public Map<String, Object> loadHomePageData(HttpServletRequest request,HttpSession session){
		Map<String,Object> map=new HashMap<String, Object>();
		Object obj = session.getAttribute("user");
		UserBean user = JSONObject.parseObject(obj.toString(), UserBean.class);
		if(user==null){
			map.put("status", false);
			map.put("info", "用户未登录，请先登录！");
			return map;
		}
		
		PageVo vo = new PageVo();
		vo.setPageIndex(1);
		vo.setPageSize(10);
		
		//1、查询该用户可查看的企业总数和项目总数
		long num = this.retrievalService.comSearch("data_enterprise", "", "AND,ent_stats,0,0", vo, user).getTotal();
		map.put("enterpriseCount", num);
		num = this.retrievalService.comSearch("data_project", "", "AND,pro_stats,0,0", vo, user).getTotal();
		map.put("projectCount", num);
		
		//2、查询公告
		vo.setOrderBy("createTime");
		vo.setOrderDesc(1);
		Object data = this.noticeService.findByNoticeTitleContaining("", vo).getRows();
		map.put("notice", data);
		
		//3、查询企业进度
		vo.setOrderBy("next_step_time");
		vo.setOrderDesc(0);
		data = this.retrievalService.messageDetails(3, vo, user).getRows();
		map.put("entProgress", data);
		
		//3、查询项目进度
		data = this.retrievalService.messageDetails(4, vo, user).getRows();
		map.put("proProgress", data);
		
		//4、查询会议记录
		vo.setOrderBy("meetTime");
		vo.setOrderDesc(1);
		
		Map<String, Object> param = new HashMap<>();
		param.put("isStats", 1);
		param.put("user_uuid", user.getUuid());
		param.put("isPower", this.roleUserService.findByRoleUuidAndUserUuid("admin", param.get("user_uuid").toString()).size()<=0);
		data = this.meetingRecordService.findAll(param, vo).getRows();
		map.put("meeting", data);
		
		return map;
	}
}
